package com.jcl.recruit.console.mapper;

import com.jcl.recruit.console.model.ConsoleDbCorp;
import com.jcl.recruit.mapper.SqlMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;


public interface ConsoleDbCorpMapper extends SqlMapper {

    String TABLE_NAME = "t_console_db_corp";

    @Select({
            SQL_QUERY_SERVER
    })
    List<ConsoleDbCorp> findList(@Param("serverName") String serverName);
    @Select({
            SQL_QUERY_SERVER_NUMBER
    })
    List<Map<String, Object>> findServerNumber();

    @Select({
            SQL_GET_ONE
    })
    ConsoleDbCorp getOne();

    @Insert({
            "<script>",
            "insert into t_console_db_corp (server_name,corp_code,status,create_time,update_time)",
            " values ",
            "<foreach collection='list' item='entity' separator=','>",
            "(#{entity.serverName},#{entity.corpCode},#{entity.status},#{entity.createTime},#{entity.updateTime})",
            "</foreach>",
            "</script>"
    })
    int batchInsert(@Param("list") List<ConsoleDbCorp> list);

    @Update({
            SQL_UPDATE_STATUS
    })
    int updateStatus(@Param("status") String status, @Param("id") Integer id);

    // 0，已经使用
    String STATUS_0 = "0";
    // 1，未启用
    String STATUS_1 = "1";

    String SQL_QUERY_SERVER = "select * from " + TABLE_NAME + " where server_name = #{serverName}";
    String SQL_QUERY_SERVER_NUMBER = "select server_name serverName, count(corp_code) number from "+TABLE_NAME
            +" group by server_name ";
    String SQL_GET_ONE = "select * from " + TABLE_NAME + " where status = " + STATUS_1
            + " order by id asc limit 1";
    String SQL_UPDATE_STATUS = "update " + TABLE_NAME + " set status = #{status} where id = #{id}";
}
